$(function() {



    // 退出登录
    $("#gaobuding1").on("click", function() {
            console.log(111);
            $("#gaobuding1").attr("href", "javascript:viod(0)")
            layer.confirm("您确定要退出登录吗?", {
                icon: 5,
                btn: ["确定", "取消"]
            }, function(index) {
                $.ajax({
                    url: "./api/quit",
                    type: "get"
                }).then(function(res) {
                    layer.msg("退出成功", {
                        icon: 6
                    })
                    setTimeout(() => {
                        window.location.href = "index.html";
                    }, 2000);


                })


                layer.close(index)
            })

        })
        // $("#borderlist").css("display", "none")


    if ($("#tab") == null) {
        console.log(1);
        $(".borderlist").hide()
    }
    if (document.cookie != null) {
        var str = document.cookie

        $("#gaobuding").html(decodeURIComponent(str).split("=")[1].split(",")[1].split(":")[1])
        if (document.cookie == null) {
            $("#gaobuding").html("123")
        } else {

            $("#gaobuding").html(JSON.parse(decodeURIComponent(str).split("=")[1].split(",")[1].split(":")[1]))

            $("#gaobuding").attr("href", "#")

            $("#gaobuding1").text("退出登录")
            console.log(322);
        }

    }


    // 渲染购物车
    $.ajax({
        url: "./api/carts",
        type: "get",
        data: {
            uid: JSON.parse($.cookie("loginInfo"))[0].uid
        }
    }).done(function(res) {
        console.log(res);
        var { data } = res
        console.log(data);
        render(data)



        console.log($("#tab").find("tr").length);
        if ($("#tab").find("tr").length == 0) {

            $("#borderlist").css("display", "none")
        } else {
            $(".border1").css("display", "none")
        }


    })


})

function render(arr) {
    var html = ""
    arr.forEach((el) => {

        html += `
       



        <tr class="list1_2">
        <td class="checkbtn">

            <input type="checkbox" class="add-value">
        </td>
        <td class="list1_sp">
            <div class="list1_sp_1 fl">

                <a href="./details.html" target="_blank"><img src=${el.p_photo} alt=""></a>
            </div>
            <div class="list1_sp_2">
                <a href="./details.html" target="_blank" title="">

                    <img src="./spimages/list2.png" alt="" style="width: 80px;height: 20px;"> <span>${el.p_name}</span>
                </a>
                <p>
                    <span style="color:#999;margin-right: 24px;
                    margin-top: 5px;">${el.u_time}</span>
                    <span style="color:#999;">${el.p_size}</span>
                </p>
            </div>
        </td>
        <td class="list1_dj"><span>${el.p_price}</span></td>
        <td data-cid=${el.c_id}>
            <div class="cart-num">
                <ul>
                    <li><a href="javascript:void(0)" class="cart-numa cart-numa2">-</a></li>
                    <li id="liudehua">
                        <input type="text" class="cartnum-value" value="${el.p_num}">
                    </li>
                    <li> <a href="javascript:void(0)" id="cart-numa1" class="cart-numa11">+</a></li>

                </ul>
            </div>
        </td>
        <td class="tab-zdcon-line"><span><p>¥0.00</p><p style="cursor:pointer"></p></span></td>
        <td class="tab-zdcon-line"><em>${Number(el.u_total).toFixed(2)}</em></td>
        <td>
            <div class="list_sc">
                <p style="color: red;">
                    <a href="javascript:void(0);" action="collect" sku_id="50230105034006" class="tabdiv-make">移入收藏夹</a>
                </p>
            </div>
            <p class="gbd"><a class="tabdiv-del" data-cid=${el.c_id} cartmaincode="1197165" cartdetailcode="3330365" action="delete" href="javascript:void(0);" title="" style="color: #000;">删除</a></p class="gbd">
        </td>
    </tr>

        `

        $(".list1").find("tbody").html(html)


    })

    // 开始
    $(".allist").on("mouseenter", function() {
        $(".hideboxfirst").css("display", "block")
    }).on("mouseleave", function() {
        $(".hideboxfirst").css("display", "none")
    })

    $(".hideboxfirst").on("mouseenter", function() {
        $(this).css("display", "block")
    })






    $(".cart-numa").on("click", function() {
        var val = $(this).parent("li").next("li").children("input").val()
        if (val == 1) {
            return
        }
        val = val - 1
        $(this).parent("li").next("li").children("input").val(val)
        $(this).parent("li").parent("ul").parent(".cart-num").parent("td").next("td").next("td").children("em").text(Number(val * $(this).parent("li").parent("ul").parent(".cart-num").parent("td").prev("td").children("span").text()).toFixed(2));
        var params = {
                cid: $(this).parent("li").parent("ul").parent(".cart-num").parent("td").data("cid"),
                pnum: val
            }
            // console.log((params));
        $.ajax({
            url: "./api/modify",
            type: "post",
            data: params
        }).then(function(res) {
            layer.msg(res.msg)
        })
        var myMoney = 0
        $("#tab").find("tr").each((index, el) => {
            myMoney += Number($(el).find("td:eq(5)").find("em").text())


        })



    })



    $(".cart-numa11").on("click", function() {
        var val = $(this).parent("li").prev("li").children("input").val()
        val = Number(val) + 1

        $(this).parent("li").prev("li").children("input").val(val)
        $(this).parent("li").parent("ul").parent(".cart-num").parent("td").next("td").next("td").children("em").text(Number(val * $(this).parent("li").parent("ul").parent(".cart-num").parent("td").prev("td").children("span").text()).toFixed(2));
        var params = {
            cid: $(this).parent("li").parent("ul").parent(".cart-num").parent("td").data("cid"),
            pnum: val
        }
        console.log((params));
        $.ajax({
            url: "./api/modify",
            type: "post",
            data: params
        }).then(function(res) {
            layer.msg(res.msg)
        })
        var myMoney = 0
        $("#tab").find("tr").each((index, el) => {
            myMoney += Number($(el).find("td:eq(5)").find("em").text())


        })

    })

    // 数量++结束

    // 删除
    $("#tab").on("click", ".tabdiv-del", function() {
            var self = this
            layer.confirm("您确定要删除吗", {
                icon: 2,
                btn: ["嗯嗯", "我不"]
            }, function() {
                $.ajax({
                    url: "./api/delete",
                    type: "post",
                    data: {
                        cid: $(self).data("cid")
                    }
                }).then(function(res) {
                    layer.msg(res.msg)
                    $(self).parents("tr").remove()
                    console.log($("#tab").find("tr").length);
                    if ($("#tab").find("tr").length == 0) {

                        $("#borderlist").css("display", "none")
                        $(".border1").css("display", "block")
                    } else {
                        $(".border1").css("display", "none")
                        $("#borderlist").css("display", "block")
                    }


                    var myMoney = 0

                    $("#tab").find("[type='checkbox']").prop('checked', $(this).prop("checked"))

                    $("#tab").find("[type='checkbox']:checked").parents("tr").each((index, el) => {
                        // console.log(el);
                        myMoney += Number($(el).find("td:eq(5)").text())

                    })
                    $(".carwindc-1").find("span").text("￥" + myMoney.toFixed(2))
                    $("#zongjia").text("￥" + myMoney.toFixed(2))
                    $("#zongjia1").text("￥" + myMoney.toFixed(2))


                })
                layer.close(index)


            })
            console.log(111);

        })
        // 全选删除


    // var myMoney = 0
    // $("#tab").find("tr").each((index, el) => {
    //     myMoney += Number($(el).find("td:eq(5)").find("em").text())


    // })
    // $(".carwindc-1").find("span").text("￥" + myMoney.toFixed(2))
    // $("#zongjia").text("￥" + myMoney.toFixed(2))
    // $("#zongjia1").text("￥" + myMoney.toFixed(2))

    $(".borderlist31").find("input").on("click", function() {
        var myMoney = 0

        $("#tab").find("[type='checkbox']").prop('checked', $(this).prop("checked"))

        $("#tab").find("[type='checkbox']:checked").parents("tr").each((index, el) => {
            // console.log(el);
            myMoney += Number($(el).find("td:eq(5)").text())

        })
        $(".carwindc-1").find("span").text("￥" + myMoney.toFixed(2))
        $("#zongjia").text("￥" + myMoney.toFixed(2))
        $("#zongjia1").text("￥" + myMoney.toFixed(2))
    })

    // var myNum = 0
    $("#tab").on("click", "[type='checkbox']", function() {
        var myMoney = 0
        $(this).parents("tbody").find("[type='checkbox']:checked").each((index, el) => {
            myMoney += Number($(el).parents("tr").find("td:eq(5)").text())
        })
        $(".borderlist31").find("input").prop("checked", $("#tab [type='checkbox']").length == $("#tab [type='checkbox']:checked").length)
            // $("#tab").find(".cartnum-value").each((index, el) => {

        //     myNum += Number($(el).val())
        // })
        // $(".carwindc-2").find("span").eq(0).text(myNum)
        $(".carwindc-1").find("span").text("￥" + myMoney.toFixed(2))
        $("#zongjia").text("￥" + myMoney.toFixed(2))
        $("#zongjia1").text("￥" + myMoney.toFixed(2))



    })






}